﻿using System;
using System.Collections.Generic;
using System.Text;
using AllegNet;
using Res = Ex04_LoadingImage;

namespace Ex04_LoadingImage
{
    class Main : Api
    {
        #region Properties
        private string _Str_ScreenResolution = "";
        private string _Str_GfxMode = "";
        private string _Str_RenderingMode = "";
        private string _Str_BmpType = "";

        public string Str_ScreenResolution
        {
            get { return _Str_ScreenResolution; }
            set { _Str_ScreenResolution = value; }
        }
        public string Str_GfxMode
        {
            get { return _Str_GfxMode; }
            set { _Str_GfxMode = value; }
        }
        public string Str_RenderingMode
        {
            get { return _Str_RenderingMode; }
            set { _Str_RenderingMode = value; }
        }
        public string Str_BmpType
        {
            get { return _Str_BmpType; }
            set { _Str_BmpType = value; }
        }
        #endregion

        internal static int Fps = 0;
        internal static int FpsToShow = 0;

        public void WinMain()
        {
            ScreenResolution ScreenRes = ScreenResolution.Size_320_240;
            GfxModes GfxMode = GfxModes.GfxAutoDetect;
            RenderingModes RenderingMode = RenderingModes.Null;
            BmpType BmpTyp = BmpType.Null;


            #region Get Params
            #region switch (Str_ScreenResolution)
            switch (Str_ScreenResolution)
            {
                case "320 x 240":
                    ScreenRes = ScreenResolution.Size_320_240;
                    break;
                case "640 x 480":
                    ScreenRes = ScreenResolution.Size_640_480;
                    break;
                case "800 x 600":
                    ScreenRes = ScreenResolution.Size_800_600;
                    break;
                case "1024 x 768":
                    ScreenRes = ScreenResolution.Size_1024_768;
                    break;
                case "1088 x 612":
                    ScreenRes = ScreenResolution.Size_1088_612;
                    break;
                case "1152 x 864":
                    ScreenRes = ScreenResolution.Size_1152_864;
                    break;
                case "1280 x 720":
                    ScreenRes = ScreenResolution.Size_1280_720;
                    break;
                case "1280 x 768":
                    ScreenRes = ScreenResolution.Size_1280_768;
                    break;
                case "1280 x 800":
                    ScreenRes = ScreenResolution.Size_1280_800;
                    break;
                case "1280 x 960":
                    ScreenRes = ScreenResolution.Size_1280_960;
                    break;
                case "1280 x 1024":
                    ScreenRes = ScreenResolution.Size_1280_1024;
                    break;
            }
            #endregion

            #region  switch (Str_GfxMode)
            switch (Str_GfxMode)
            {
                case "GfxAutoDetect":
                    GfxMode = GfxModes.GfxAutoDetect;
                    break;
                case "GfxAutoDetectFullScreen":
                    GfxMode = GfxModes.GfxAutoDetectFullScreen;
                    break;
                case "GfxAutoDetectWindowed":
                    GfxMode = GfxModes.GfxAutoDetectWindowed;
                    break;
                case "GfxDirectXAccel":
                    GfxMode = GfxModes.GfxDirectXAccel;
                    break;
                case "GfxDirectXWin":
                    GfxMode = GfxModes.GfxDirectXWin;
                    break;
                case "GfxDirectX":
                    GfxMode = GfxModes.GfxDirectX;
                    break;
                case "GfxDirectXSafe":
                    GfxMode = GfxModes.GfxDirectXSafe;
                    break;
                case "GfxDirectXSoft":
                    GfxMode = GfxModes.GfxDirectXSoft;
                    break;
                case "GfxDirectXOlv":
                    GfxMode = GfxModes.GfxDirectXOlv;
                    break;
                case "GfxGDI":
                    GfxMode = GfxModes.GfxGDI;
                    break;
            }
            #endregion

            #region switch (Str_RenderingMode)
            switch (Str_RenderingMode)
            {
                case "DoubleBuffering":
                    RenderingMode = RenderingModes.DoubleBuffering;
                    break;
                case "TripleBuffering":
                    RenderingMode = RenderingModes.TripleBuffering;
                    break;
                case "PageFlipping":
                    RenderingMode = RenderingModes.PageFlipping;
                    break;

            }
            #endregion

            #region switch (Str_BmpType)
            switch (Str_BmpType)
            {
                case "Memory":
                    BmpTyp = BmpType.Memory;
                    break;
                case "System":
                    BmpTyp = BmpType.System;
                    break;
                case "Video":
                    BmpTyp = BmpType.Video;
                    break;

            }
            #endregion
            #endregion

            //Initialize AllegNet
            AllegNetInit();

            //Install Keyboard
            InstallKeyboard();

            //Install Timer
            InstallTimer();

            InstallMouse();

            //Set the application running on background
            SetDisplaySwitchMode(SwitchModes.SwitchBackground);

            //Set the render mode
            SetRenderMode(RenderingMode, BmpTyp);

            //Set the colorDepth
            SetColorDepth(CDepth.Depth_32_Bits); //32 Bits cause only this mode can accept ARGB

            //Set the Gfx Mode
            SetGfxMode(GfxMode, ScreenRes);


            Show8BitColor();
            ShowTrueColor();
            ShowTrueColorJpg();
            ShowTrueColorAlpha();

            
            //Exit AllegNet
            UninstallAllegNet();
        }


        #region 8 bit color
        public void Show8BitColor()
        {
            while (KbKey[AllKeys.Enter]) ;

            //Initialize main loop condition
            bool Done = false;

            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                TextOutEx(BufferedScreen, DefaultFont, "Loadding 8 Bit color image", 10, 10, MakeCol(255, 255, 255), 0);
                TextOutEx(BufferedScreen, DefaultFont, "Press 'Enter' key to continue...", 10, 25, MakeCol(255, 255, 255), 0);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }
            Palette P = new Palette();
            Bmp Color8Bit = LoadBitmap("Color8Bit.bmp", ref P);

            while (KbKey[AllKeys.Enter]) ;

            Done = false;
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                Blit(Color8Bit, BufferedScreen, 0, 0, 0, 0, Color8Bit.Width, Color8Bit.Height);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }
        } 
        #endregion

        #region True color Bmp
        public void ShowTrueColor()
        {
            while (KbKey[AllKeys.Enter]) ;

            //Initialize main loop condition
            bool Done = false;

            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                TextOutEx(BufferedScreen, DefaultFont, "Loadding True color image", 10, 10, MakeCol(255, 255, 255), 0);
                TextOutEx(BufferedScreen, DefaultFont, "Press 'Enter' key to continue...", 10, 25, MakeCol(255, 255, 255), 0);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }

            Bmp TrueColor = LoadBitmap("TrueColor.bmp");

            while (KbKey[AllKeys.Enter]) ;


            Done = false;
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                Blit(TrueColor, BufferedScreen, 0, 0, 0, 0, TrueColor.Width, TrueColor.Height);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }
        } 
        #endregion

        #region True color Jpg
        public void ShowTrueColorJpg()
        {
            while (KbKey[AllKeys.Enter]) ;

            //Initialize main loop condition
            bool Done = false;

            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                TextOutEx(BufferedScreen, DefaultFont, "Loadding True color image Jpg", 10, 10, MakeCol(255, 255, 255), 0);
                TextOutEx(BufferedScreen, DefaultFont, "Press 'Enter' key to continue...", 10, 25, MakeCol(255, 255, 255), 0);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }

            Bmp TrueColor = LoadTrueColorPicture("TrueColor.Jpg");

            while (KbKey[AllKeys.Enter]) ;


            Done = false;
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                Blit(TrueColor, BufferedScreen, 0, 0, 0, 0, TrueColor.Width, TrueColor.Height);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }
        } 
        #endregion

        #region True color with Alpha Transparency
        public void ShowTrueColorAlpha()
        {
            while (KbKey[AllKeys.Enter]) ;

            //Initialize main loop condition
            bool Done = false;

            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                TextOutEx(BufferedScreen, DefaultFont, "Loadding True color image and apply alpha transparecy", 10, 10, MakeCol(255, 255, 255), 0);
                TextOutEx(BufferedScreen, DefaultFont, "Press 'Enter' key to continue...", 10, 25, MakeCol(255, 255, 255), 0);

                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }

            Bmp Alpha01 = LoadTrueColorPicture("Alpha01.png");
            Bmp Alpha02 = LoadTrueColorPicture("Alpha02.png");

            while (KbKey[AllKeys.Enter]) ;

            //So we set the drawing mode to DrawModeTrans
            //there is no pattern here to apply so we pass null and 0 0 for the rest
            //This functino only affect drawing method like putpixel, but not Blit or draw sprite
            DrawingMode(DrawingModes.DrawModeTrans, null, 0, 0);

            //Now with this function enable, we can directly draw the alpha chanel with the put pixel function
            //Only value between 0-255 are valid.
            SetWriteAlphaBlender();



            for (int x = 0; x < Alpha02.Width; x++)
            {
                for (int y = 0; y < Alpha02.Height; y++)
                {
                    int Alpha = 50;

                    PutPixel(Alpha02, x, y, Alpha);
                }
            }


            //Now we allow drawing Bmp with Alpha chanel
            //Using drawSprite
            SetAlphaBlender();

            Done = false;
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);

                //DrawTransSprite(BufferedScreen, MySha, 0,0);
                DrawTransSprite(BufferedScreen, Alpha01, (ScreenWidth - Alpha01.Width) / 2, (ScreenHeight - Alpha01.Height) / 2);
                //DrawTransSprite(BufferedScreen, MySha, 0,0);
                DrawTransSprite(BufferedScreen, Alpha02, (ScreenWidth - Alpha02.Width) / 2, (ScreenHeight - Alpha02.Height) / 2);


                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                //Check for KeyPressed
                if (KbKey[AllKeys.Enter])
                    Done = true;
            }
        } 
        #endregion
    }
}
